Passed
Push — master ( 1adf40...f067da )
by lv
01:05
created

statistics.js ➔ ???   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 54
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 31
nc 8
dl 0
loc 54
c 0
b 0
f 0
rs 8.6693
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
const Constant = require('../../libraries/constant')
0 ignored issues
show
Unused Code introduced by
The constant Constant seems to be never used. Consider removing it.
Loading history...
2
const Moment = require('moment')
3
const Redis = require('../../../config/db').redis
4
const ModelStatisticsStore = require('../../../app/model/mist/statisticsStore')
5
6
var INSERT_PER_TIMES = 1000
7
8
var statistics = async function () {
9
    let accessDate = Moment().subtract(1, 'days').format('YYYY-MM-DD')
10
    let statisticsDay = Moment().subtract(1, 'days').format('YYYYMMDD')
11
    // let accessDate = Moment().format('YYYY-MM-DD')
12
    // let statisticsDay = Moment().format('YYYYMMDD')
13
    let uvCachePrefix = 'MIST:ST_UV:DATE:' + statisticsDay + ':STORE_ID:'
14
    let pvCacheKey = 'MIST:ST_PV:DATE:' + statisticsDay
15
16
    // 根据storeID前缀 1-9 
17
    let keywords
18
    let uvKeys
19
20
    // insert update data
21
    let insertData = []
22
23
    for (let index = 1; index < 10; index++) {
24
        keywords = uvCachePrefix + index.toString() + '*'
25
        uvKeys = await Redis.keys(keywords)
26
        for (let i = 0; i < uvKeys.length; i++) {
27
            element = uvKeys[i]
0 ignored issues
show
Bug introduced by
The variable element seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.element.
Loading history...
28
            let storeId = element.replace(uvCachePrefix, '')
29
30
            let tmpUv = await Redis.bitcount(element)
31
            let tmpPv = await Redis.hget(pvCacheKey, storeId)
32
33
            let tmp = {
34
                store_id: storeId,
35
                access_date: accessDate,
36
                pv: tmpPv,
37
                uv: tmpUv,
38
            }
39
            // console.log(tmp)
40
            // console.log('---------------------------')
41
42
            insertData.push(tmp)
43
            // 每1000条插入一次
44
            if (insertData.length >= INSERT_PER_TIMES) {
45
                console.log('---- insert DB ---- count: ' + insertData.length)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
46
                ModelStatisticsStore.add(insertData)
47
                insertData.length = 0
48
            }
49
            
50
        }
51
    }
52
53
    if (insertData.length > 0) {
54
        console.log('---- last insert DB ---- count: ' + insertData.length)
55
        await ModelStatisticsStore.add(insertData)
56
    }
57
58
    // update total
59
    await ModelStatisticsStore.updateStatisticsTotal(accessDate)
60
    console.log('ok statistics.js ' + Moment().format('YYYYMMDD HH:mm:ss'))
61
}
62
63
module.exports = statistics